package com.heima.redisson.controller;

import com.heima.redisson.config.RedissonConfig;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class DelayTaskController {

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("putTask/{value}/{delaySeconds}")
    public String putTask(@PathVariable("value")String value,
                          @PathVariable("delaySeconds")Integer delaySeconds){
        // 先获取BlockingDeque
        RBlockingDeque<String> demoQueue = redissonClient.getBlockingDeque("demo_queue");
        // 再获取DelayedQueue
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(demoQueue);
        // 向delayedQueue发送数据
        delayedQueue.offer(value, delaySeconds, TimeUnit.SECONDS);
        log.info("延迟任务入队:{}", value );
        return value + ":" +delaySeconds;
    }

}
